package com.example.health;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class WebServiceHandler {
	public WebServiceHandler() {
		super();
		// TODO Auto-generated constructor stub
	}

	public WebServiceHandler(String uRL) {
		super();
		URL = uRL;
	}

	private String URL = "http://the/url/here";
	private String result = "";
	private String deviceId = "xxxxx";
	private final String tag = "webLog";

	public String getURL() {
		return URL;
	}

	public void setURL(String uRL) {
		URL = uRL;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getTag() {
		return tag;
	}
	
	public JSONArray callWebService(String q) throws JSONException{  
        HttpClient httpclient = new DefaultHttpClient();  
        HttpGet request = new HttpGet(URL + q);  
        request.addHeader("deviceId", deviceId);  
        ResponseHandler<String> handler = new BasicResponseHandler(); 
        try {  
           result = httpclient.execute(request, handler);  
        } catch (ClientProtocolException e) {  
            System.out.println("ClientProtocolException: "+e.getMessage());  
        } catch (IOException e) { 
            System.out.println("IOException: "+e.getMessage());  
        }  
        httpclient.getConnectionManager().shutdown();   
        System.out.println(result);  
        return new JSONArray(result);
    } // end callWebService()  
}
